const utils           = require('../../../utils');
const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class TrialBackendHanlder extends BaseHandler {

    get pattern() {
        return "/request-trial";
    }

    doPOST(request, response) {
        var data = request.body;
        var error = 'success';
        if (data.security_code !== request.session.user.security_code) {
            error = "非法的访问，请通过正常途径进行试用申请！";
        } else if (!data.email ||(data.email && !utils.verifyEmailAddress(data.email))) {
            error = "请输入合法的邮件地址！";
        } else if(!data.industry) {
            error = "请选择一个行业！";
        }

        if (error !== 'success') {
            response.json({
                success: false,
                error: error
            });
        } else {
            data.refs = request.session.user.refs;
            this.Controller.createTrialUser(request.headers.host, data, (result) => {
                response.json(result);
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = TrialBackendHanlder;